package com.pfzy.lib.sdk.permission;

import android.app.Activity;
import android.content.Context;

import com.pfzy.lib.sdk.permission.callback.PermissionInterface;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class UXPermission {
    private Activity mActivity;
    private List<String> mPermissions = new ArrayList();

    private UXPermission(Activity activity) {
        this.mActivity = activity;
    }

    public static UXPermission with(Activity activity) {
        return new UXPermission(activity);
    }

    public UXPermission permission(String... permissions2) {
        this.mPermissions.addAll(Arrays.asList(permissions2));
        return this;
    }

    public UXPermission permission(String[]... permissions2) {
        for (String[] group : permissions2) {
            this.mPermissions.addAll(Arrays.asList(group));
        }
        return this;
    }

    public UXPermission permission(List<String> permissions2) {
        this.mPermissions.addAll(permissions2);
        return this;
    }

    /**
     * 请求权限
     * @param call
     */
    public void request(PermissionInterface call) {
        if (this.mPermissions == null || this.mPermissions.size() == 0) {
            this.mPermissions = PermissionUtils.getManifestPermissions(this.mActivity);
        }
        if (this.mPermissions == null || this.mPermissions.size() == 0) {
            throw new IllegalArgumentException("The requested permission cannot be empty");
        }
        if (this.mActivity == null) {
            throw new IllegalArgumentException("The activity is empty");
        }
        if (call == null) {
            throw new IllegalArgumentException("The permission request callback interface must be implemented");
        }
        if (!PermissionUtils.isOverMarshmallow()) {
            call.needCheckPermission();
            return;
        }
        PermissionUtils.checkTargetSdkVersion(this.mActivity, this.mPermissions);
        ArrayList<String> failPermissions = PermissionUtils.getFailPermissions(this.mActivity, this.mPermissions);
        if (failPermissions == null || failPermissions.size() == 0) {
            call.hasPermission(this.mPermissions, true);
        } else {
            PermissionUtils.checkPermissions(this.mActivity, this.mPermissions);
            PermissionFragment.newInstant(this.mActivity, new ArrayList(this.mPermissions)).prepareRequest(this.mActivity, call);
        }
        this.mActivity = null;
    }

    /**
     * 具有权限
     * @param context
     * @param permissions2
     * @return
     */
    public static boolean isHasPermission(Context context, String... permissions2) {
        ArrayList<String> failPermissions = PermissionUtils.getFailPermissions(context, Arrays.asList(permissions2));
        return failPermissions == null || failPermissions.size() == 0;
    }

    public static boolean isHasPermission(Context context, String[]... permissions2) {
        List<String> permissionList = new ArrayList<>();
        for (String[] group : permissions2) {
            permissionList.addAll(Arrays.asList(group));
        }
        ArrayList<String> failPermissions = PermissionUtils.getFailPermissions(context, permissionList);
        return failPermissions == null || failPermissions.size() == 0;
    }

    /**
     * 转到权限设置
     * @param context
     */
    public static void gotoPermissionSettings(Context context) {
        PermissionSettingPage.start(context, false, false);
    }

    public static void gotoDefaultPermissionSettings(Context context) {
        PermissionSettingPage.start(context, false, true);
    }

    public static void gotoPermissionSettings(Context context, boolean newTask) {
        PermissionSettingPage.start(context, newTask, false);
    }
}
